module.exports = function ($scope, $modalInstance, FileUploader,$resource, $modal, item, flag, toaster){
    
    $scope.groupList = [];
    $scope.openMap = {};
    $scope.temp_group = {
        isReadly: false,
        code: "",
        name: ""
    };
    $scope.load = function () {

        $resource('/api/as/sc/office/getDistributorList', {}, {}).save({}, function (resultOne) {
            if (resultOne.errcode === 0) {
                $scope.companyList = resultOne.data;
                $scope.loadGroupList();
            } else {
                toaster.error({ title: '', body: resultOne.errmsg });
            }
        });
    }

    $scope.loadGroupList = function () {
        $resource('/api/as/sc/office/findDownCompanyGroupList', {}, {}).save($scope.temp_group, function (res) {
            if (res.errcode === 0) {
                $scope.groupList = res.data;
                $scope.groupList.push({
                    floating_price: 0,
                    code: 'weifenzu',
                    name: '未分组',
                    childList: []
                })
                for(var child_list in $scope.groupList){
                    $scope.groupList[child_list].childList = [];
                    for(var child in $scope.companyList){
                        for(iii in $scope.info.companycodes){
                            if($scope.info.companycodes[iii].code == $scope.companyList[child].code){
                                $scope.companyList[child].check = true;
                            }
                        }
                        if($scope.companyList[child].group_code ==  $scope.groupList[child_list].code){
                            $scope.groupList[child_list].childList.push($scope.companyList[child]);
                        }
                    }
                    $scope.groupList[child_list].is_open = false;
                }
                console.log($scope.groupList)
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }
   
    $scope.open_toggle = function (index) {
        $scope.groupList[index].is_open = !$scope.groupList[index].is_open;
    }

    
    init();
    function init(){
        $scope.info = flag ? {'flag': true} : angular.copy(item);
        $scope.load();
       
        
    }


    //保存
    $scope.save = function () {
        $scope.info.companycodes = [];
        for(f in  $scope.groupList){
            for(s in $scope.groupList[f].childList){
                if($scope.groupList[f].childList[s].check){
                    $scope.info.companycodes.push($scope.groupList[f].childList[s]);
                }
            }
        }
        $modalInstance.close($scope.info);
    }


     //组选择
     $scope.groupChecked = function (index) {
        $scope.groupList[index].check = !$scope.groupList[index].check;
        for(i in $scope.groupList[index].childList){
            $scope.groupList[index].childList[i].check = $scope.groupList[index].check;
        }
    }

    //全选
    $scope.allChecked = function () {
        $scope.check = !$scope.check;
        for(index in  $scope.groupList){
            $scope.groupList[index].check = !$scope.groupList[index].check;
            for(i in $scope.groupList[index].childList){
                $scope.groupList[index].childList[i].check = $scope.groupList[index].check;
            }
        }
    }


    

    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }
};